package com.sphd.restaurantsystem.model;


import com.android.volley.VolleyError;
import com.sphd.restaurantsystem.info.RestaurantInfo;
import com.sphd.restaurantsystem.interfaces.VolleyJsonObjectCallBack;
import com.sphd.restaurantsystem.presenter.UserHomePresenter;
import com.sphd.restaurantsystem.presenter.UserHomePresenterImpl;
import com.wzh.frame.utils.JsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Hu on 2016/11/02
 */

public class UserHomeModelImpl implements UserHomeModel, VolleyJsonObjectCallBack {
    UserHomePresenter userHomePresenter;

    @Override
    public void senUrl(String url, UserHomePresenter userHomePresenter) {
        volleyUtil.setJsonObjectRequest(url, this);
        this.userHomePresenter = userHomePresenter;
    }

    @Override
    public void sendMoneyUrl(String s,UserHomePresenter userHomePresenter) {
        volleyUtil.setJsonObjectRequest(s, new VolleyJsonObjectCallBack() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    double sumtopup=response.getDouble("sumtopup");
                    double account=response.getDouble("account");
                    double daytopup=response.getDouble("daytopup");
                    double sumorder=response.getDouble("sumorder");
                    double dayorder=response.getDouble("dayorder");
                    userHomePresenter.sendMoney(sumtopup,account,daytopup,sumorder,dayorder);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
userHomePresenter.sendError(error);
            }
        });

    }

    @Override
    public void sendResUrl(String s,UserHomePresenter userHomePresenter) {
        volleyUtil.setJsonObjectRequest(s, new VolleyJsonObjectCallBack() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    List<RestaurantInfo> restaurantInfos=JsonUtil.getListFromString(response.getString("res"),RestaurantInfo.class);
                    userHomePresenter.sendFundingRes(restaurantInfos);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
            userHomePresenter.sendError(error);
            }
        });
    }

    @Override
    public void addRest(String s, UserHomePresenterImpl userHomePresenter) {
        volleyUtil.setJsonObjectRequest(s, new VolleyJsonObjectCallBack() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    int res=response.getInt("res");
                    userHomePresenter.recRes(res);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
    }

    @Override
    public void onResponse(JSONObject response) {
        try {
            List<RestaurantInfo> restaurantInfos = JsonUtil.getListFromString(response.getString("res"), RestaurantInfo.class);
            int resNo = response.getInt("number");
            userHomePresenter.sendRes(restaurantInfos, resNo);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        userHomePresenter.sendError(error);
    }
}